package com.sykj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.pojo.Customer;
import com.sykj.servies.CustomerServies;
import com.sykj.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    CustomerServies customerServies;
    /***
     * 后台显示用户
     */
    @RequestMapping("/list")
    public  String list(Model model, Page page){
        //分页查询
        PageHelper.offsetPage(page.getStart(),page.getCount());
        //继承公共Dao方法
        List<Customer> list= customerServies.list();
        int total = (int) new PageInfo<>(list).getTotal();//总条数
        page.setTotal(total);
        //保存在Modle
        model.addAttribute("list",list);
        model.addAttribute("totals",total);
        //返回给视图界面
        return "cstpage/cst-list";
    }

    //修改是否成为会员

    /**
     *通过id修改状态
     * @param id
     * @return
     */
    @RequestMapping("/shezhihuiyuan")
    @ResponseBody
    public String shezhihuiyuan(int id){
        customerServies.shezhihuiyuan(id);
        return "success";
    }

    /**
     *根据id删除用户
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public String del(int id){
        customerServies.del(id);
        return "redirect:list";
    }
}
